package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.UUID;

/**
 * @author 刘昱江
 * 时间 2021/5/11
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;



    @Override
    public String findUserUp(User user) {

        String password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(password);
        QueryWrapper<User>  queryWrapper = new QueryWrapper<>(user);
        User  u1 =userMapper.selectOne(queryWrapper);
        return  u1==null? null: UUID.randomUUID().toString().replace("-", "");

    }

    @Override
    public PageResult getUserList(PageResult pageResult) {
        IPage<User> iPage = new Page<>(pageResult.getPageNum(), pageResult.getPageSize());
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasLength(pageResult.getQuery()), "username", pageResult.getQuery());
           iPage =  userMapper.selectPage(iPage,queryWrapper);
        return new PageResult().setTotal(iPage.getTotal()).setRows(iPage.getRecords());
    }

    @Override
    public void changeStatus(User user) {
                userMapper.updateById(user);
    }
}
